const spiralOrder = matrix =>{
    const res = [];
    const [m,n] = [matrix.length,matrix[0]?.length];
    if(!m || !n){
        return res;
    }
    // 四个边界 值
    let [left,right,up,down] = [0,n-1,0,m-1];

    while(1){
        // 向右
        for(let j = left;j<=right;j++) res.push(matrix[up][j]);
        up++;
        if(up>down) break;
        // 向下
        for(let i = up;i<=down;i++) res.push(matrix[i][right]);
        right--;
        if(up>down) break;
        // 向左
        for(let j = right;j>=left;j--) res.push(matrix[down][j]);
        down--;
        if(left>right) break;
        // 向上
        for(let i = down;i>=up;i--) res.push(matrix[i][left]);
        left++;
        if(left>right) break;
    }
    return res;
}

console.log(spiralOrder([[1,2,3],[4,5,6],[7,8,9]]));